package interfaceGraphique;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JTextArea;
import javax.swing.JButton;

import metier.*;

import facade.FacadeAnneeEtude;

public class GestionFormationGUI extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JLabel lChoix = null;
	private JComboBox listeFormation = null;
	private JLabel lNom = null;
	private JTextField tNom = null;
	private JLabel lDescription = null;
	private JTextArea taDescription = null;
	private JPanel boutonPan = null;
	private JButton bAjout = null;
	private JButton bModifier = null;
	private JButton bSupprimer = null;
	private FacadeAnneeEtude facade=new FacadeAnneeEtude();
	private JPanel jPanelFormation = null;
	private ElementListe elNouvelleFormation=null;

	/**
	 * This is the default constructor
	 */
	public GestionFormationGUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
		gridBagConstraints10.insets = new Insets(5, 79, 47, 80);
		gridBagConstraints10.gridy = 0;
		gridBagConstraints10.gridx = 0;
		this.setLayout(new GridBagLayout());
		this.setSize(new Dimension(800, 350));
		this.add(getJPanelFormation(), gridBagConstraints10);
		this.bModifier.setEnabled(false);
		this.bSupprimer.setEnabled(false);
	}

	/**
	 * This method initializes listeFormation	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getListeFormation() {
		if (listeFormation == null) {
			ArrayList<ElementListe> liste = facade.creerListeFormation();
			liste.add(0, getElNouvelleFormation());
			listeFormation = new JComboBox(liste.toArray());
			listeFormation.setPreferredSize(new Dimension(200,20));
			listeFormation.setMinimumSize(new Dimension(200,20));
			listeFormation.addActionListener(this);
		}
		return listeFormation;
	}
	
	private ElementListe getElNouvelleFormation() {
		if (this.elNouvelleFormation == null) {
			this.elNouvelleFormation = new ElementListe(0, "Nouvelle Formation");	
		}
		return elNouvelleFormation;
	}

	/**
	 * This method initializes tNom	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTNom() {
		if (tNom == null) {
			tNom = new JTextField();
			tNom.setPreferredSize(new Dimension(200,20));
			tNom.setMinimumSize(new Dimension(200,20));
			
		}
		return tNom;
	}

	/**
	 * This method initializes taDescription	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getTaDescription() {
		if (taDescription == null) {
			taDescription = new JTextArea();
			taDescription.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			taDescription.setPreferredSize(new Dimension(300,150));
			taDescription.setMinimumSize(new Dimension(300,150));

		}
		return taDescription;
	}

	/**
	 * This method initializes boutonPan	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBoutonPan() {
		if (boutonPan == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 2;
			gridBagConstraints4.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints4.gridy = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.insets = new Insets(0, 0, 10, 10);
			gridBagConstraints3.gridy = 0;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new Insets(0, 0, 10, 10);
			gridBagConstraints2.gridy = 0;
			boutonPan = new JPanel();
			boutonPan.setLayout(new GridBagLayout());
			boutonPan.add(getBAjout(), gridBagConstraints2);
			boutonPan.add(getBModifier(), gridBagConstraints3);
			boutonPan.add(getBSupprimer(), gridBagConstraints4);
		}
		return boutonPan;
	}

	/**
	 * This method initializes bAjout	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBAjout() {
		if (bAjout == null) {
			bAjout = new JButton();
			bAjout.setText("Ajouter");
			bAjout.addActionListener(this);
		}
		return bAjout;
	}

	/**
	 * This method initializes bModifier	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBModifier() {
		if (bModifier == null) {
			bModifier = new JButton();
			bModifier.setText("Modifier");
			bModifier.addActionListener(this);
		}
		return bModifier;
	}

	/**
	 * This method initializes bSupprimer	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBSupprimer() {
		if (bSupprimer == null) {
			bSupprimer = new JButton();
			bSupprimer.setText("Supprimer");
			bSupprimer.addActionListener(this);
		}
		return bSupprimer;
	}

	/**
	 * This method initializes jPanelFormation	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelFormation() {
		if (jPanelFormation == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints9.gridwidth = 2;
			gridBagConstraints9.gridx = 2;
			gridBagConstraints9.gridy = 3;
			gridBagConstraints9.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.anchor = GridBagConstraints.NORTHWEST;
			gridBagConstraints8.insets = new Insets(10, 10, 20, 10);
			gridBagConstraints8.gridheight = 1;
			gridBagConstraints8.gridwidth = 2;
			gridBagConstraints8.gridx = 2;
			gridBagConstraints8.gridy = 2;
			gridBagConstraints8.weightx = 0.0;
			gridBagConstraints8.weighty = 0.0;
			gridBagConstraints8.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.anchor = GridBagConstraints.NORTH;
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.insets = new Insets(10, 10, 0, 10);
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints6.gridx = -1;
			gridBagConstraints6.gridy = 1;
			gridBagConstraints6.weightx = 0.0;
			gridBagConstraints6.weighty = 0.0;
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = -1;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.insets = new Insets(0, 10, 0, 10);
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(5, 10, 5, 10);
			gridBagConstraints1.gridx = -1;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 0.0;
			gridBagConstraints1.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.insets = new Insets(10, 10, 0, 0);
			jPanelFormation = new JPanel();
			jPanelFormation.setBorder(BorderFactory.createTitledBorder("Gestion des formations"));
			lDescription = new JLabel();
			lDescription.setText("Description :");
			lNom = new JLabel();
			lNom.setText("Nom :");
			lChoix = new JLabel();
			lChoix.setText("Choix formation :");
			jPanelFormation.setLayout(new GridBagLayout());
			jPanelFormation.add(lChoix, gridBagConstraints);
			jPanelFormation.add(getListeFormation(), gridBagConstraints1);
			jPanelFormation.add(lNom, gridBagConstraints5);
			jPanelFormation.add(getTNom(), gridBagConstraints6);
			jPanelFormation.add(lDescription, gridBagConstraints7);
			jPanelFormation.add(getTaDescription(), gridBagConstraints8);
			jPanelFormation.add(getBoutonPan(), gridBagConstraints9);
		}
		return jPanelFormation;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(listeFormation)){
			if (listeFormation.getSelectedItem()!=null){
				if (listeFormation.getSelectedItem().equals(this.getElNouvelleFormation())){
					bAjout.setEnabled(true);
					bModifier.setEnabled(false);
					bSupprimer.setEnabled(false);
					tNom.setText("");
					taDescription.setText("");
					refreshListe ();
				}
				else{
					facade.creerFormation("", "");
					try {
						facade.getFormation().load(((ElementListe)listeFormation.getSelectedItem()).getNom());
						tNom.setText(facade.getFormation().getNom());
						taDescription.setText(facade.getFormation().getDescription());
					} catch (ExceptionMetier e1) {
						e1.printStackTrace();
					}
					bAjout.setEnabled(false);
					bModifier.setEnabled(true);
					bSupprimer.setEnabled(true);
				}
			}
		}
		else if (e.getSource().equals(bAjout)){
			facade.creerFormation(tNom.getText(), taDescription.getText());
			try {
				facade.ajouterFormation();
			} catch (ExceptionMetier e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			refreshListe ();
		}
		else if (e.getSource().equals(bModifier)){
			facade.getFormation().setNom(tNom.getText());
			facade.getFormation().setDescription(taDescription.getText());
			try {
				facade.getFormation().update();
			} catch (ExceptionMetier e1) {
				e1.printStackTrace();
			}
			
			refreshListe();
		}
		else if (e.getSource().equals(bSupprimer)){
			facade.getFormation().setNom(tNom.getText());
			facade.getFormation().setDescription(taDescription.getText());
			try {
				facade.getFormation().delete();
			} catch (ExceptionMetier e1) {
				e1.printStackTrace();
			}
			listeFormation.removeAllItems();
			refreshListe();
		}
		
	}
	
	public void refreshListe (){
		ArrayList<ElementListe> liste = facade.creerListeFormation();
		liste.add(0, getElNouvelleFormation());
		listeFormation.removeAllItems();
		for(int i = 0; i < liste.size(); i++) {getListeFormation().addItem(liste.get(i));}
	}
}  //  @jve:decl-index=0:visual-constraint="6,72"
